package handlers

import (
	"context"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
	"github.com/twwch/gin-sdk/handler"
	"github.com/twwch/gin-sdk/server"
	"net/http"
	deploySvc "twwch/k8s-admin/application/deploy"
	"twwch/k8s-admin/models/common"
	"twwch/k8s-admin/models/deploy"
)

type DeployHandler struct {
	handler.Base
	deploySvc *deploySvc.DeployService
}

func (h *DeployHandler) Init(ginRouter *gin.RouterGroup) {
	h.Logger = log.WithField("handler", "deploy")
	h.deploySvc = deploySvc.GetService()
	// registry http handler
	if ginRouter != nil {
		appGroup := ginRouter.Group("/deploy")
		server.Route(appGroup, http.MethodPost, "/create", h.Create)
		server.Route(appGroup, http.MethodGet, "/delete", h.Delete)
		server.Route(appGroup, http.MethodPost, "/update", h.Update)
		server.Route(appGroup, http.MethodGet, "/get", h.Get)
		server.Route(appGroup, http.MethodGet, "/list", h.List)
		//appGroup.GET("/", warper.CreateHandlerFunc(h.Test, false))
	}
}

// curl 127.0.0.1:8081/v1/deploy/create -X POST -d '{"name": "test", "namespace": "test", "replicas": 1, "containers": [{"name": "nginx80", "image": "nginx:1.12", "ports": [{"name": "http", "protocol": "TCP", "containerPort": 80}]}]}' -H 'Content-Type: application/json; charset=utf-8'
func (h *DeployHandler) Create(ctx context.Context, req *deploy.CreateReq) (*deploy.CreateResp, error) {
	return h.deploySvc.CreateDeploy(ctx, req)
}

func (h *DeployHandler) Delete(ctx context.Context, req *deploy.DeleteReq) (*common.Empty, error) {
	return h.deploySvc.DeleteDeploy(ctx, req)
}

func (h *DeployHandler) Update(ctx context.Context, req *deploy.UpdateReq) (*deploy.UpdateResp, error) {
	return h.deploySvc.UpdateDeploy(ctx, req)
}

func (h *DeployHandler) Get(ctx context.Context, req *deploy.GetReq) (*deploy.GetResp, error) {
	return h.deploySvc.GetDeploy(ctx, req)
}

func (h *DeployHandler) List(ctx context.Context, req *deploy.ListReq) (*deploy.ListResp, error) {
	return h.deploySvc.ListDeploy(ctx, req)
}
